package com.eqdd.yiqidian.ui.gongneng.renshi.qiyewenhua;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.EditImgActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.QiyeImgBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import android.text.TextUtils;
import android.view.View;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.schedulers.Schedulers;

public class EditImgActivity extends BaseActivity {
    private EditImgActivityCustom dataBinding;
    private QiyeImgBean qiyeImgBean;
    String imgPath;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_edit_img);
        initTopTitleBar(View.VISIBLE, "编辑");
        initTopRightText("更新", v -> {
            PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.QIYE_IMG_UPDATE)
                    .params("userGuid", user.getGuid())
                    .params("companyId", user.getCompanyId())
                    .params("url", dataBinding.spinner.getSelectedItem().toString()+getParam(dataBinding.etUrl.getText().toString()))
                    .params("title", getParam(dataBinding.etTitle.getText().toString()))
                    .params("sort", qiyeImgBean.getSort())
                    .params("imageId", qiyeImgBean.getId());
            if (!TextUtils.isEmpty(imgPath)) {
                params.params("file", new File(imgPath));
            }

            params.execute(new DialogCallBack<HttpResult>(EditImgActivity.this) {
                @Override
                public void onSuccess(Response<HttpResult> response) {
                    HttpResult httpResult = response.body();
                    ToastUtil.showShort(httpResult.getMsg());
                    if (httpResult.getStatus() == 200) {
                        setResult(Config.SUCCESS);
                        finish();
                    }
                }
            });
        });
    }

    private String getParam(String param) {
        return TextUtils.isEmpty(param) ? "" : param;
    }

    @Override
    public void initData() {
        qiyeImgBean = (QiyeImgBean) getIntent().getSerializableExtra("bean");
    }

    @Override
    public void setView() {

        dataBinding.etTitle.setText(qiyeImgBean.getTitle());
        dataBinding.etUrl.setText(qiyeImgBean.getUrl());
        ImageUtil.setImage(qiyeImgBean.getImageUrl(), dataBinding.ivImg);
        RxView.clicks(dataBinding.ivImg)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(v -> {
                    PicUtil.cut(EditImgActivity.this, 1000, 500);
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
            imgPath = localMedia.get(0).getCompressPath();
            ImageUtil.setImage(imgPath, dataBinding.ivImg);

        }
    }
}
